﻿using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Http;
using System.Linq;
using System.Reflection.Metadata;

namespace CKS2320.Common.Utils
{
    public static class GetApiUtils
    {
        /// <summary>
        /// POST请求API
        /// </summary>
        /// <param name="request"></param>
        /// <param name="url"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        public static async Task<string> GetPostApi(string request, string url, string token = "")
        {
            var httpClient = new HttpClient();
            if (!string.IsNullOrEmpty(token))
                httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("bearer", token.Split(' ')[1]);
            using HttpContent httpContent = new StringContent(request, System.Text.Encoding.UTF8, "application/json");
            var response = await httpClient.PostAsync(url, httpContent);
            var content = await response.Content.ReadAsStringAsync();
            return content;
        }
        /// <summary>
        /// GET请求API
        /// </summary>
        /// <param name="url"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        public static async Task<string> GetApi(string url, string token = "")
        {
            var httpClient = new HttpClient();
            if (!string.IsNullOrEmpty(token))
                httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("bearer", token.Split(' ')[1]);
            var response = await httpClient.GetAsync(url);
            var content = await response.Content.ReadAsStringAsync();
            return content;
        }
        //public static async Task<string> GetApi(string request, string url, string token, IHttpClientFactory _httpClientFactory)
        //{
        //    HttpClient client = new HttpClient();

        //    var httpclient = _httpClientFactory.CreateClient();
        //    if (!string.IsNullOrEmpty(token))
        //        httpclient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("bearer", token.Split(' ')[1]);
        //    using HttpContent httpContent = new StringContent(request, System.Text.Encoding.UTF8, "application/json");
        //    string urlStr = "localhost:7164/" + url;
        //    var result = await httpclient.PostAsync(url, httpContent);
        //    var content = await result.Content.ReadAsStringAsync();
        //    return content;
        //}
    }
}
